package com.jiliang.common.area;

/**
 * 获取两个经纬度坐标点之间距离
 * @author 
 * @version v_1.0.1
 * @since 2019/6/04 09:21
 */
public class DistributionDsUtils {
    /**
     * 起点经度
     **/
    private double startLongitude;
    /**
     * 起点纬度
     **/
    private double startLatitude;

    /**
     * 终点经度
     **/
    private double endLongitude;
    /**
     * 终点纬度
     **/
    private double endLatitude;

    /**
     * 设置起点经纬度坐标
     * @param startLongitude 起点经度坐标
     * @param startLatitude 起点纬度坐标
     * @author 
     * @since 2019/6/4 9:45
     **/
    public void setStartPoint(double startLongitude, double startLatitude) {
        this.startLongitude = startLongitude;
        this.startLatitude = startLatitude;
    }

    /**
     * 设置终点经纬度坐标
     * @param endLongitude 终点经度坐标
     * @param endLatitude 终点纬度坐标
     * @author 
     * @since 2019/6/4 9:45
     **/
    public void setEndPoint(double endLongitude, double endLatitude) {
        this.endLongitude = endLongitude;
        this.endLatitude = endLatitude;
    }

    public double getDistance() {
        return getDistance(this.startLongitude, this.startLatitude, this.endLongitude, this.endLatitude);
    }

    public double getDistance(double endLongitude, double endLatitude) {
        return getDistance(this.startLongitude, this.startLatitude, endLongitude, endLatitude);
    }

    public static double getDistance(double startLongitude, double startLatitude, double endLongitude, double endLatitude)
    {
        double lon1 = (Math.PI / 180) * startLongitude;
        double lon2 = (Math.PI / 180) * endLongitude;
        double lat1 = (Math.PI / 180) * startLatitude;
        double lat2 = (Math.PI / 180) * endLatitude;
        //地球半径
        double r = 6371;
        //两点间距离（单位：km）
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * r;
        //返回距离（单位：m）
        return d * 1000;
    }
}
